package demo.bean.util;


import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class ScanUtil {

    //path获取项目的路径
    private static final String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();

    //创建一个集合封装类名
    private static List<String> list = new ArrayList<String>();

    //返回所有一个结果集
    public static List<String> scanf(String packageName){
        //绝对路径+包名（转换后）
        readFile(path+packageName.replace(".","/"));
        return list;
    }

    private static void readFile(String classPath){

        File file = new File(classPath);
        File[] files = file.listFiles();
        for (File f:files){
            if (f.isFile()){
                String className = readName(f.getAbsolutePath());
                list.add(className);
            }else{
                readFile(f.getAbsolutePath());
            }
        }
    }

    //后缀名去掉和斜杠转换成点
    private static String readName(String classPath){
        String className = classPath.substring(path.length()-1, classPath.length());
        className = className.replace(".class","").replace("\\",".");
        return className;
    }

    /*public static void main(String[] args) {
        List<String> scanf = ScanUtil.scanf("demo.bean");
        for (String s:scanf){
            System.out.println(s);
        }
    }*/

}
